#!/usr/bin/python
# -*- coding: utf-8 -*-  
"""
@Project : hello 
@file : oracle_to_csv_icinl.py
@Author : sheen
@time : 2025/5/20 11:45
@func : 单个耗时SQl查询
"""
import logging
import time

from com.cn.for_cdc.check_data.oracle_to_csv import write_dicts_to_csv
from com.cn.for_cdc.common.OracleQueryExecutor import OracleQueryExecutor
from com.cn.for_cdc.common.cdc_conmons import oracle_adm_connections
from com.cn.for_cdc.common.log_helper import configure_logging


def main():
    """使用示例"""
    configure_logging()

    # 初始化执行器
    config = oracle_adm_connections['icibe_pro']
    executor = OracleQueryExecutor(config)

    # 复杂查询示例
    complex_sql = """
SELECT /*+ parallel(16) */scx.ROW_ID, ATTRIB_12 AS EFFECTIVE_START_DT, ATTRIB_13 AS EFFECTIVE_END_DT
FROM siebel.S_CONTACT@SBL sc , siebel.S_CONTACT_XM@SBL scx 
WHERE sc.ROW_ID = scx.PAR_ROW_ID AND BU_ID = '1-7V7V'  
AND sc.LAST_UPD < sysdate -1 / 12
MINUS 
SELECT /*+ parallel(16) */CONTACT_ATTRIBUTE_ID , EFFECTIVE_START_DT , EFFECTIVE_END_DT 
FROM STG_B_CONTACT_ATTR sbca       
    """

    # 执行单个查询
    try:
        start = time.time()
        result = executor.execute_query(complex_sql)
        logging.info(f"获取到{len(result)}条差异数据")
        file_name = r'F:\workspace\pythonProject\hello\com\cn\for_cdc\output_data\20250522_icinl_17.csv'
        write_dicts_to_csv(data=result, filename=file_name)
        logging.info(f"总执行耗时: {time.time() - start:.2f}s")
    except Exception as e:
        logging.error(f"主流程执行异常: {e}")


if __name__ == "__main__":
    main()
